﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcAtletIS.Models;

namespace MvcAtletIS.Areas.Admin.Controllers
{
    [Authorize(Roles = "Administrators")]
    public class DocumentsController : Controller
    {
        private atletisEntities db = new atletisEntities();

        // GET: /Admin/Documents/
        public ViewResult Index(int SubcategoryId)
        {
            ViewBag.ParentCategory = db.DocumentsSubcategories.Single(ds => ds.ID == SubcategoryId).DocumentsCategories;
            ViewBag.ParentSubcategory = db.DocumentsSubcategories.Single(ds => ds.ID == SubcategoryId);
            var documents = db.Documents.Include("DocumentsSubcategories").Where(d => d.SubcategoryId == SubcategoryId);
            return View(documents.ToList());
        }

        // GET: /Admin/Documents/Details/5
        public ViewResult Details(int id)
        {
            Documents documents = db.Documents.Single(d => d.ID == id);
            return View(documents);
        }

        // GET: /Admin/Documents/Create
        public ActionResult Create(int SubcategoryId)
        {
            Documents model = new Documents();
            DocumentsSubcategories dSubcategory = db.DocumentsSubcategories.SingleOrDefault(ds => ds.ID == SubcategoryId);
            DocumentsCategories dCategory = db.DocumentsCategories.SingleOrDefault(dc => dc.ID == dSubcategory.CategoryId);

            model.SubcategoryId = SubcategoryId;
            model.Path = dCategory.Subfolder + "\\" + dSubcategory.Subfolder + "\\";
            return View(model);
        } 

        // POST: /Admin/Documents/Create
        [HttpPost]
        public ActionResult Create(Documents documents)
        {
            if (ModelState.IsValid)
            {
                db.Documents.AddObject(documents);
                db.SaveChanges();
                return RedirectToAction("Index", new { SubcategoryId = documents.SubcategoryId});  
            }

            return View(documents);
        }
        
        // GET: /Admin/Documents/Edit/5
        public ActionResult Edit(int id)
        {
            Documents documents = db.Documents.Single(d => d.ID == id);
            ViewBag.SubcategoryId = new SelectList(db.DocumentsSubcategories, "ID", "Name", documents.SubcategoryId);
            return View(documents);
        }

        // POST: /Admin/Documents/Edit/5
        [HttpPost]
        public ActionResult Edit(Documents documents)
        {
            if (ModelState.IsValid)
            {
                db.Documents.Attach(documents);
                db.ObjectStateManager.ChangeObjectState(documents, EntityState.Modified);
                db.SaveChanges();
                return RedirectToAction("Index", new { SubcategoryId = documents.SubcategoryId });
            }
            ViewBag.SubcategoryId = new SelectList(db.DocumentsSubcategories, "ID", "Name", documents.SubcategoryId);
            return View(documents);
        }

        // GET: /Admin/Documents/Delete/5
        public ActionResult Delete(int id)
        {
            Documents documents = db.Documents.Single(d => d.ID == id);
            return View(documents);
        }

        // POST: /Admin/Documents/Delete/5
        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            Documents documents = db.Documents.Single(d => d.ID == id);
            int subcategoryId = Convert.ToInt32(documents.SubcategoryId);

            db.Documents.DeleteObject(documents);
            db.SaveChanges();
            return RedirectToAction("Index", new { SubcategoryId = subcategoryId });
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}